package top.zywork.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.AppAuthFilter;
import top.zywork.annotation.SysLog;
import top.zywork.annotation.XssFilter;
import top.zywork.common.TransactionNoGenerator;
import top.zywork.security.JwtUser;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsOrderService;
import top.zywork.service.PayNotifyService;
import top.zywork.service.PayService;
import top.zywork.vo.ResponseStatusVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * <p>微信支付控制器</p>
 * 创建于2019-07-19<br/>
 *
 * @author 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/wx-pay")
@Slf4j
@XssFilter(ignoreUrls = {"/wx-pay/goods-order/wx-pay-notify"})
@AppAuthFilter(ignoreUrls = {"/wx-pay/goods-order/wx-pay-notify"})
public class WeixinPayController extends BaseController {

    private PayService payService;

    private GoodsOrderService goodsOrderService;

    private PayNotifyService payNotifyService;

    /**
     * 商城订单公众号发起支付
     * @param orderIdAndNos
     * @return
     */
    @GetMapping("goods-order/gzh-pay-request/{orderIdAndNos}")
    @SysLog(description = "商城订单公众号发起支付请求")
    public ResponseStatusVO goodsOrderWxGzhPayRequest(@PathVariable("orderIdAndNos") String orderIdAndNos) {
        String[] orderIdAndNoAry = orderIdAndNos.split(",");
        int length = orderIdAndNoAry.length;
        Long[] orderIdAry = new Long[length];
        String[] orderNoAry = new String[length];
        for (int i = 0; i < length; i++) {
            String[] idNo = orderIdAndNoAry[i].split("-");
            orderIdAry[i] = Long.valueOf(idNo[0]);
            orderNoAry[i] = idNo[1];
        }
        Long count = goodsOrderService.countOrderNos(orderNoAry);
        if (count != orderIdAry.length) {
            return ResponseStatusVO.dataError("支付订单号有误", null);
        }
        JwtUser jwtUser = SecurityUtils.getJwtUser();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", jwtUser.getUserId());
        jsonObject.put("orderIdAndNos", orderIdAndNos);
        BigDecimal totalPay = goodsOrderService.calculateTotalAmount(orderNoAry);
        return payService.payByGzhWxpay(jwtUser.getUsername(), TransactionNoGenerator.generateOrderNo(), "127.0.0.1",
                "商城订单支付", jsonObject.toJSONString(), totalPay.multiply(new BigDecimal(100)).intValue(), null);
    }

    /**
     * 商城订单小程序发起支付
     * @param orderIdAndNos 订单编号和订单号，之间用-隔开，如1-8899,2-798687
     * @return
     */
    @GetMapping("goods-order/xcx-pay-request/{orderIdAndNos}")
    @SysLog(description = "商城订单小程序发起支付请求")
    public ResponseStatusVO goodsOrderWxWcxPayRequest(@PathVariable("orderIdAndNos") String orderIdAndNos) {
        String[] orderIdAndNoAry = orderIdAndNos.split(",");
        int length = orderIdAndNoAry.length;
        Long[] orderIdAry = new Long[length];
        String[] orderNoAry = new String[length];
        for (int i = 0; i < length; i++) {
            String[] idNo = orderIdAndNoAry[i].split("-");
            orderIdAry[i] = Long.valueOf(idNo[0]);
            orderNoAry[i] = idNo[1];
        }
        Long count = goodsOrderService.countOrderNos(orderNoAry);
        if (count != orderIdAry.length) {
            return ResponseStatusVO.dataError("支付订单号有误", null);
        }
        JwtUser jwtUser = SecurityUtils.getJwtUser();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", jwtUser.getUserId());
        jsonObject.put("orderIdAndNos", orderIdAndNos);
        BigDecimal totalPay = goodsOrderService.calculateTotalAmount(orderNoAry);
        return payService.payByXcxWxpay(jwtUser.getUsername(), TransactionNoGenerator.generateOrderNo(), "127.0.0.1",
                "商城订单支付", jsonObject.toJSONString(), totalPay.multiply(new BigDecimal(100)).intValue(), null);
    }

    /**
     * 商城微信支付回调
     * @param request
     * @param response
     */
    @PostMapping("goods-order/wx-pay-notify")
    public void wxGoodsOrderPayNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.payByWxNotify(request, response, payNotifyService);
    }

    @Autowired
    public void setPayService(PayService payService) {
        this.payService = payService;
    }

    @Autowired
    public void setGoodsOrderService(GoodsOrderService goodsOrderService) {
        this.goodsOrderService = goodsOrderService;
    }

    @Autowired
    public void setPayNotifyService(PayNotifyService payNotifyService) {
        this.payNotifyService = payNotifyService;
    }
}
